package com.example.demo.dto;

import com.example.demo.entity.TaskApprovalStatus;
import com.fasterxml.jackson.annotation.JsonInclude;
import java.time.LocalDateTime;

@JsonInclude(JsonInclude.Include.ALWAYS)
public class TaskApprovalWithRawDataDTO {
    private Long id;                    // 主键ID
    private Long taskFlowId;            // 关联task_flow表的ID
    private Long nodeId;                // 节点ID（process_node.id）
    private Integer status;             // 状态：0=待处理,1=处理中,2=已通过,3=已拒绝,4=已跳过
    private String statusText;          // 状态文本描述
    private Long approverId;            // 审批人ID
    private String approverName;        // 审批人姓名
    private String approverAuthId;      // 审批人认证ID
    private String comment;             // 审批意见
    private LocalDateTime startTime;    // 节点开始时间
    private LocalDateTime endTime;      // 节点完成时间
    private LocalDateTime createdAt;    // 创建时间
    private LocalDateTime updatedAt;    // 更新时间
    private Long taskId;                // 任务ID
    private Long dataSourceId;          // 数据源ID
    private Long flowId;                // 流程ID
    private String flowName;            // 流程名称（从process_config表获取）
    private String nodeName;            // 节点名称（从process_node表获取）
    private String rawData;             // 从数据源抓取的原始数据，JSON格式
    private String extraFields;         // 从data_source表获取的额外扩展字段，JSON格式
    private String nodeContent;         // 从process_node表获取的节点内容（表单配置等），JSON格式
    private Long taskType;              // 任务类型ID（从task_type表获取）
    private String taskTypeName;        // 任务类型名称（从task_type表获取）
    private String taskDescription;     // 任务类型描述（从task_type表获取）

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Long getTaskFlowId() {
        return taskFlowId;
    }

    public void setTaskFlowId(Long taskFlowId) {
        this.taskFlowId = taskFlowId;
    }

    public Long getNodeId() {
        return nodeId;
    }

    public void setNodeId(Long nodeId) {
        this.nodeId = nodeId;
    }

    public Integer getStatus() {
        return status;
    }

    public void setStatus(Integer status) {
        this.status = status;
    }

    public String getStatusText() {
        return statusText;
    }

    public void setStatusText(String statusText) {
        this.statusText = statusText;
    }

    public Long getApproverId() {
        return approverId;
    }

    public void setApproverId(Long approverId) {
        this.approverId = approverId;
    }

    public String getApproverName() {
        return approverName;
    }

    public void setApproverName(String approverName) {
        this.approverName = approverName;
    }

    public String getApproverAuthId() {
        return approverAuthId;
    }

    public void setApproverAuthId(String approverAuthId) {
        this.approverAuthId = approverAuthId;
    }

    public String getComment() {
        return comment;
    }

    public void setComment(String comment) {
        this.comment = comment;
    }

    public LocalDateTime getStartTime() {
        return startTime;
    }

    public void setStartTime(LocalDateTime startTime) {
        this.startTime = startTime;
    }

    public LocalDateTime getEndTime() {
        return endTime;
    }

    public void setEndTime(LocalDateTime endTime) {
        this.endTime = endTime;
    }

    public LocalDateTime getCreatedAt() {
        return createdAt;
    }

    public void setCreatedAt(LocalDateTime createdAt) {
        this.createdAt = createdAt;
    }

    public LocalDateTime getUpdatedAt() {
        return updatedAt;
    }

    public void setUpdatedAt(LocalDateTime updatedAt) {
        this.updatedAt = updatedAt;
    }

    public Long getTaskId() {
        return taskId;
    }

    public void setTaskId(Long taskId) {
        this.taskId = taskId;
    }

    public Long getDataSourceId() {
        return dataSourceId;
    }

    public void setDataSourceId(Long dataSourceId) {
        this.dataSourceId = dataSourceId;
    }

    public Long getFlowId() {
        return flowId;
    }

    public void setFlowId(Long flowId) {
        this.flowId = flowId;
    }

    public String getFlowName() {
        return flowName;
    }

    public void setFlowName(String flowName) {
        this.flowName = flowName;
    }

    public String getNodeName() {
        return nodeName;
    }

    public void setNodeName(String nodeName) {
        this.nodeName = nodeName;
    }

    public String getRawData() {
        return rawData;
    }

    public void setRawData(String rawData) {
        this.rawData = rawData;
    }

    public String getExtraFields() {
        return extraFields;
    }

    public void setExtraFields(String extraFields) {
        this.extraFields = extraFields;
    }

    public String getNodeContent() {
        return nodeContent;
    }

    public void setNodeContent(String nodeContent) {
        this.nodeContent = nodeContent;
    }

    public Long getTaskType() {
        return taskType;
    }

    public void setTaskType(Long taskType) {
        this.taskType = taskType;
    }

    public String getTaskTypeName() {
        return taskTypeName;
    }

    public void setTaskTypeName(String taskTypeName) {
        this.taskTypeName = taskTypeName;
    }

    public String getTaskDescription() {
        return taskDescription;
    }

    public void setTaskDescription(String taskDescription) {
        this.taskDescription = taskDescription;
    }

    // 从实体类转换为DTO
    public static TaskApprovalWithRawDataDTO fromEntity(TaskApprovalStatus entity) {
        TaskApprovalWithRawDataDTO dto = new TaskApprovalWithRawDataDTO();
        dto.setId(entity.getId());
        dto.setTaskFlowId(entity.getTaskFlowId());
        dto.setNodeId(entity.getNodeId());
        dto.setStatus(entity.getStatus());
        dto.setStatusText(getStatusText(entity.getStatus()));
        dto.setApproverId(entity.getApproverId());
        dto.setApproverName(entity.getApproverName());
        dto.setApproverAuthId(null);    // 在Service层中会实际获取该值
        dto.setComment(entity.getComment());
        dto.setStartTime(entity.getStartTime());
        dto.setEndTime(entity.getEndTime());
        dto.setCreatedAt(entity.getCreatedAt());
        dto.setUpdatedAt(entity.getUpdatedAt());
        dto.setTaskId(entity.getTaskId());
        dto.setDataSourceId(entity.getDataSourceId());
        dto.setFlowId(entity.getFlowId());
        // 直接设置默认的rawData
        dto.setRawData("{\"test\": \"raw-data\", \"taskId\": " + entity.getTaskId() + "}");
        // 设置默认的extraFields
        dto.setExtraFields("{}");
        // 设置默认的nodeContent
        dto.setNodeContent("{}");
        // 设置默认的taskType、taskTypeName和taskDescription（实际应用中应该通过dataSourceId查询获取）
        dto.setTaskType(null);
        dto.setTaskTypeName("未知类型");
        dto.setTaskDescription("未知描述");
        return dto;
    }

    // 状态文本映射
    private static String getStatusText(Integer status) {
        if (status == null) return "未知";
        switch (status) {
            case 0: return "待处理";
            case 1: return "处理中";
            case 2: return "已通过";
            case 3: return "已拒绝";
            case 4: return "已跳过";
            default: return "未知";
        }
    }
}